package com.glory.brepda.activity.mfg;


import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.glory.brepda.R;
import com.glory.brepda.adapter.ChangelblPackageAdapter;
import com.glory.brepda.base.BaseActivity;
import com.glory.brepda.callback.CustomDialogEntityCallBack;
import com.glory.brepda.entity.ChangelblPackageEntity;
import com.glory.brepda.entity.response.BaseResponseBean;
import com.glory.brepda.utils.NetUtils;
import com.google.gson.JsonObject;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.model.Response;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_EntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_LogUtils;
import com.pda.platform.ui.ui_pdaplatform.view.FreeUI_ClearEditText;

import butterknife.BindView;
import butterknife.OnClick;

public class ChangelblPackageActivity extends BaseActivity {

    @BindView(R.id.tvTitle)
    TextView tvTitle;
    @BindView(R.id.tvBaseSubmit)
    TextView tvBaseSubmit;
    @BindView(R.id.llMain)
    LinearLayout llMain;
    @BindView(R.id.etPackLotID)
    FreeUI_ClearEditText etPackLotID;
    @BindView(R.id.etLotID)
    FreeUI_ClearEditText etLotID;
    @BindView(R.id.tvQty)
    TextView tvQty;
    @BindView(R.id.rvChangelblPackageList)
    RecyclerView rvChangelblPackageList;

    private ChangelblPackageAdapter mChangelblPackageAdapter;

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_changelblpackage;
    }

    @Override
    protected void initView() {
        tvTitle.setText("包装信息转移");
        setMainLayout(llMain);
        tvBaseSubmit.setVisibility(View.GONE);

        initRecyclerView(rvChangelblPackageList, this);
        mChangelblPackageAdapter = new ChangelblPackageAdapter(null);
        mChangelblPackageAdapter.openLoadAnimation(BaseQuickAdapter.ALPHAIN);
        mChangelblPackageAdapter.isFirstOnly(true);
        rvChangelblPackageList.setAdapter(mChangelblPackageAdapter);
    }

    @Override
    protected void initData() {
        initEdit(etPackLotID,s ->{
            if (getText(etLotID).equals("")){
                showToast("请扫描批次号",false);
                etLotID.requestFocus();
                return;
            }
            FreeUI_EntityCallBack<BaseResponseBean<Object>> callBack = new CustomDialogEntityCallBack<BaseResponseBean<Object>>
                    (new TypeToken<BaseResponseBean<Object>>() {
                    }.getType(),getSupportFragmentManager(), this) {

                @Override
                public void onSuccess(Response<BaseResponseBean<Object>> response) {
                    super.onSuccess(response);
                    if (response.body().isSuccess(ChangelblPackageActivity.this, false, getSupportFragmentManager())) {
                        showToast("提交成功",true);
                        ChangelblPackageEntity changelblPackageEntity = new ChangelblPackageEntity();
                        changelblPackageEntity.setPackLotID(getText(etPackLotID));
                        changelblPackageEntity.setLotId(getText(etLotID));
                        mChangelblPackageAdapter.addData(changelblPackageEntity);
                        tvQty.setText("数量："+mChangelblPackageAdapter.getData().size());
                        etPackLotID.setText("");
                        etPackLotID.requestFocus();
                    }
                }

                @Override
                public void onError(Response<BaseResponseBean<Object>> response) {
                    super.onError(response);
                    FreeApi_LogUtils.saveErrorMessages(response.getException(), "CHANGELBLPACKAGE");
                }
            };
            JsonObject map = new JsonObject();
            map.addProperty("ACTIONTYPE", "CHANGE");
            map.addProperty("LOTID", getText(etLotID));
            map.addProperty("PACKLOTID", getText(etPackLotID));
            NetUtils.requestPostNet("IF.CHANGELBLPACKAGE", "Common", ChangelblPackageActivity.this, map, callBack);

        });
    }
    @OnClick({R.id.tvBaseSubmit})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tvBaseSubmit:
                break;
        }
    }
}